package com.qiuzhiyi.activity;

import android.support.design.widget.BottomSheetDialog;
import android.view.LayoutInflater;
import android.view.View;

import com.li.utils.CacheUtil;
import com.li.widget.DoubleTextView;
import com.li.widget.PersonalDetailItem;
import com.li.widget.TopNavigationBar;
import com.qiuzhiyi.BuildConfig;
import com.qiuzhiyi.R;
import com.qiuzhiyi.base.QZYBaseActivity;
import com.qiuzhiyi.cache.HumanCache;
import com.qiuzhiyi.item.AboutUsItem;
import com.qiuzhiyi.item.BaseItem;
import com.qiuzhiyi.net.ObservableHelper;
import com.qiuzhiyi.net.RetrofitHelper;
import com.qiuzhiyi.net.SelectorObserver;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * Author: Li
 * Date: 2017/11/14 0014
 * Description: 设置界面
 */

public class SettingActivity extends QZYBaseActivity {

    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.txt_version)
    PersonalDetailItem mTxtVersion;
    @BindView(R.id.txt_phone)
    PersonalDetailItem mTxtPhone;
    @BindView(R.id.txt_cache)
    PersonalDetailItem mTxtCache;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mTopNavigation.setNavigationBarListener(this);
        String phone = mItem.getHumanAccount();
        mTxtPhone.setTxtRight(phone.substring(0, 3) + "****" + phone.substring(7));
        mTxtCache.setTxtRight(CacheUtil.getTotalCacheSize(SettingActivity.this));
        mTxtVersion.setTxtRight("V" + BuildConfig.VERSION_NAME);
    }


    @OnClick({R.id.txt_logout, R.id.txt_cache, R.id.txt_about})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_logout:
                QMUIDialog.MessageDialogBuilder builder = new QMUIDialog.MessageDialogBuilder(mContext)
                        .setTitle("退出提示")
                        .setMessage("是否确认退出登录")
                        .addAction("取消", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();
                            }
                        })
                        .addAction("确定", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
//                                Intent logoutIntent = new Intent(mContext, LoginActivity.class);
//                                logoutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//                                App.finishAll();
//                                App.clearActivity();
                                HumanCache.clearHumanInfo(mContext);
//                                startActivity(logoutIntent);

                                start(LoginActivity.class);
                                finish();
                            }
                        });
                builder.create().show();
                break;
            case R.id.txt_cache:
                new QMUIDialog.MessageDialogBuilder(mContext)
                        .setTitle("温馨提示")
                        .setMessage("是否清除缓存")
                        .addAction("取消", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();
                            }
                        })
                        .addAction("确定", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();
                                CacheUtil.clearAllCache(SettingActivity.this);
                                mTxtCache.setTxtRight(CacheUtil.getTotalCacheSize(SettingActivity.this));
                            }
                        }).create().show();
                break;
            case R.id.txt_about:
                getAboutUsInfo();
                break;

        }
    }

    private void getAboutUsInfo() {
        ObservableHelper.createObservable(RetrofitHelper.createService().getAboutUsInfo())
                .subscribe(new SelectorObserver<BaseItem<AboutUsItem>>() {
                    @Override
                    protected void onErrorCodeInfo(String code, String info) {
                        showToast(info);

                    }

                    @Override
                    protected void onSuccess(BaseItem<AboutUsItem> item) {

                        BottomSheetDialog sheetDialog = new BottomSheetDialog(mContext);
                        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_about_us, null);
                        DoubleTextView name = view.findViewById(R.id.txt_name);
                        DoubleTextView company = view.findViewById(R.id.txt_company);
                        DoubleTextView phone = view.findViewById(R.id.txt_phone);
                        DoubleTextView email = view.findViewById(R.id.txt_email);
                        DoubleTextView address = view.findViewById(R.id.txt_address);
                        AboutUsItem about = item.getObjValue();

                        name.setTxtRight(about.getCompany_person());
                        company.setTxtRight(about.getCompany_name());
                        phone.setTxtRight(about.getCompany_phone());
                        email.setTxtRight(about.getCompany_email());
                        address.setTxtRight(about.getCompany_address());
                        sheetDialog.setContentView(view);
                        sheetDialog.show();
                    }

                    @Override
                    protected void onFinish() {
                        dismissLoadingDialog();
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                        showLoadingDialog();
                    }
                });
    }
}
